//
//  ViewController.m
//  21-NetWork
//
//  Created by dllo on 16/8/11.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *netWorkImageView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"点击");
}
// 同
- (IBAction)syncConect:(id)sender {
    NSURL *imageURL = [NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"GET";
    NSURLResponse *respomse = nil;
    NSError *error = nil;
    // 同步请求
    // 参数1: 请求 Request
    // 参数2: response 指针的地址
    // 参数3: error 指针的地址
   NSData *data = [NSURLConnection sendSynchronousRequest:imageURLRequest returningResponse:&respomse error:&error];
    if (data) {
        _netWorkImageView.image = [UIImage imageWithData:data];
    }
}
// 异
- (IBAction)asyncConect:(id)sender {
    NSURL *imageURL = [NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"GET";
   [NSURLConnection sendAsynchronousRequest:imageURLRequest queue:[NSOperationQueue new] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
       
       _netWorkImageView.image = [UIImage imageWithData:data];
   }];
}
- (IBAction)URLSessionAsync:(id)sender {
    NSURL *imageURL = [NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"GET";
    // NSURLSession 网络请求
    // 参数1 : request
    // 参数2: Block块, callBack回调
    // 注意: Session的回调处于异步线程, 需要返回主线程刷新UI (图片赋值, tableView collectionView的刷新)
    NSURLSession *urlSession = [NSURLSession sharedSession];
  NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:imageURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
      // 主线程执行图片加载
      dispatch_async(dispatch_get_main_queue(), ^{
         _netWorkImageView.image = [UIImage imageWithData:data];
      }) ;
    }];
    // 让dataTask 执行
    [dataTask resume];
    // suspend 取消
}
- (void)creat {
NSURLSession *urlSession = [NSURLSession sharedSession];
NSURL *imageURL = [NSURL URLWithString:@"http://open3.bantangapp.com/base/app/init"];
NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
imageURLRequest.HTTPMethod = @"POST";
imageURLRequest.HTTPBody = [@"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17" dataUsingEncoding:NSUTF8StringEncoding];
// URLSession, 网络请求
// 参数1: request
// 参数2: Block块, CallBack回调
// 注意: Session的回调中处于异步线程, 需要返回主线程刷新UI(图片赋值, TableView CollectionView的刷新)
NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:imageURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    // 主线程执行图片加载
    dispatch_async(dispatch_get_main_queue(), ^{
        //            _networkImage.image = [UIImage imageWithData:data];
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@", result);
    });
}];
// 让 dataTask 执行
[dataTask resume];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end



















